


var IScollerConfig = {
    pullDown:{name:'pullDown',label:'pullDownLabel',all:'pullDown .pullDownLabel'},
	pullUp:{name:'pullUp',label:'pullUpLabel',all:'pullUp .pullUpLabel'},
	loading:{name:'loading2'},
	result:{name:'filter_result'},
	divName:{name:'wrapper'},
	emptyDiv:{name:'emptyDiv',sorry:"很遗憾，未找到你需要的节目"},
	Up:{More:'往上拉加载更多...',Release:'释放继续加载...',Loading:'正在加载...',NoMore:'没有更多了...'},
    Down:{More:'往下拉加载...',Release:'释放加载...',Loading:'正在加载...',NoMore:'到顶了...'}
};


function TouchScoller(config,callback){
	//var   data   =   eval( '( '   +   jsonData   +   ') ');
	this.config = config;
	//this.divName = data.divName;
	//this.pageCount = 0;
	this.pageCount = config.pageCount;
	this.currentPage = 1;
	this.firstPage = 1;
	this.url = '';
	this.htmlData = '';
	this.callback = callback;
	
	this.pullDownAction = function() {
		if(this.firstPage == 1){
			$('#' + this.config.pullDown.name).css('display','block');
			$('#' + this.config.pullDown.all).html(this.config.Down.NoMore);
			return 0;
		}
		var gotoNo = this.firstPage - 1;
		this.firstPage = gotoNo;
		if(gotoNo >= 1){
			$('#' + this.config.pullDown.name).css('display','block');
			this.callback.call(this,gotoNo);
			//this.showFilter(gotoNo);
			if(gotoNo == 1){
				$('#' + this.config.pullDown.all).html(this.config.Down.NoMore);
			}
		}else{
			$('#' + this.config.pullDown.name).attr('class','');
			$('#' + this.config.pullDown.all).html(this.config.Down.NoMore);
		}
	};

	this.pullUpAction = function() {
		if(this.currentPage >= this.pageCount){
			$('#' + this.config.pullUp.name).css('display','block');
			$('#' + this.config.pullUp.all).html(this.config.Up.NoMore);
			return 0;
		}else{
			var gotoNo = this.currentPage +1;
			this.currentPage = gotoNo;
			
			$('#' + this.config.pullUp.name).css('display','block');
			this.callback.call(this,gotoNo);
			if(this.currentPage == this.pageCount){
				$('#' + this.config.pullUp.all).html(this.config.Up.NoMore);
			}
		}
	};
	this.hideNoMore = function (){
		if(this.currentPage == this.pageCount){
			$('#' + this.config.pullUp.all).html(this.config.Up.NoMore);
		}else{
			$('#' + this.config.pullUp.all).html(this.config.Up.More);
		}
		//$('#' + this.config.pullUp.name).css('display','block');
	}
	this.programsScroll = null;
	this.load = function(){
		pullDownEl = document.getElementById(this.config.pullDown.name);
		pullDownOffset = pullDownEl.offsetHeight;
		pullUpEl = document.getElementById(this.config.pullUp.name);
		pullUpOffset = pullUpEl.offsetHeight;
		ele = this;
		this.programsScroll = new iScroll(this.config.divName.name, {
			scrollbarClass: "myscrollbar",
			useTransition: true,
			topOffset: pullDownOffset,
			upFlag : false,
			downFlag : false,
			onRefresh: function () {
				if(pullDownEl.className.match('_loading')) {
					pullDownEl.className = '';
					$('#'+ele.config.pullDown.name).css('display','none');
				} else if(pullUpEl.className.match('_loading')) {
					pullUpEl.className = '';
					pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.More;
					if(ele.pageCount > ele.currentPage){
						pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.More;
					}else{
						pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.NoMore;
					}
				}
			},
			onScrollMove: function () {
				var hc = $("#loadingToast").css('display');
				if(hc && hc == "block"){
					return;
				}
				if(this.scrollerH < this.wrapperH + 50){
					if(this.y >= -50){
						if(!this.downFlag){
							this.downFlag = true;
						}
						this.upFlag = false;
					}else{
						if(!this.upFlag) {
							var that = this;
							//  var _height = $('#subMenu').outerHeight();
							var _top;
							if(this.currentPage == 1){
								_top = (that.scrollerH - that.wrapperH)*-1;
							}else{
								_top = that.y;
							}
							this.upFlag = true;
						}
						this.downFlag = false;
					}
				}else{
					if(this.y >= -50){
						if(!this.downFlag){
							this.downFlag = true;
						}
						this.upFlag = false;
					}
				}
            
				if(this.y > 5 && !pullDownEl.className.match('flip')) {
					if(!this.downEnabled || ele.pageCount == 0) return;
					pullDownEl.className = 'flip';
					$('#'+ele.config.pullDown.name).css('display','block');
					
					if(ele.firstPage == 1){
						pullDownEl.querySelector('.'+ele.config.pullDown.label).innerHTML = ele.config.Down.NoMore;
					}else{
						showLoading(ele.config.Down.Loading);
						pullDownEl.querySelector('.'+ele.config.pullDown.label).innerHTML = ele.config.Down.Release;
					}
					this.minScrollY = 0;
				} else if(this.y < 5 && pullDownEl.className.match('flip')) {
					if(!this.downEnabled) return;
					pullDownEl.className = '';
					pullDownEl.querySelector('.'+ele.config.pullDown.label).innerHTML = ele.config.Down.More;
					this.minScrollY = -pullDownOffset;
				} else if(this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
					if(!this.upEnabled || ele.pageCount == 0) return;
					pullUpEl.className = 'flip';
					$('#'+ele.config.pullUp.name).css('display','block');
					if(ele.currentPage == ele.pageCount){
						pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.NoMore;
					}else{
						showLoading(ele.config.Up.Loading);
						pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.Release;
						
					}
					this.maxScrollY = this.maxScrollY;
				} else if(this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
					if(!this.upEnabled) return;
					pullUpEl.className = '';
					pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.More;
					this.maxScrollY = pullUpOffset;
				}

			},
			onScrollEnd: function () {
				$("#loadingToast").hide();
				$('#'+ele.config.pullDown.name).css('display','none');
				if(this.scrollerH > this.wrapperH + 50){
					if(this.y >= -50){
						if(!this.downFlag){
							var that = this;
							// var _height = $('#subMenu').outerHeight();
							this.downFlag = true;
						}
						this.upFlag = false;
					}else{
						if(this.currentPage != 1 && this.y > -100){
							ele.pullUpAction();
							this.upFlag = true;
						}
						this.downFlag = false;
					}
				}
				if(pullDownEl.className.match('flip')) {
					//
					if(!this.downEnabled) return;
					pullDownEl.className = '_loading';
					pullDownEl.querySelector('.'+ele.config.pullDown.label).innerHTML = ele.config.Down.Loading;
					ele.pullDownAction();   // Execute custom function (ajax call?)
					ele.refershDiv();
					//setTimeout('ele.refershDiv()',500);
				} else if(pullUpEl.className.match('flip')) {
					//
					if(!this.upEnabled) return;
					pullUpEl.className = '_loading';
					pullUpEl.querySelector('.'+ele.config.pullUp.label).innerHTML = ele.config.Up.Loading;
					ele.pullUpAction(); // Execute custom function (ajax call?)
					ele.refershDiv();
					//setTimeout('ele.refershDiv()',500);
				}
				setTimeout(function(){$("img.lazyImg").lazyload()},500);
			}
		});
		this.callback.call(this,this.currentPage);
	};
	
	this.refershDiv = function(){
		this.programsScroll.refresh();
	};
	
	this.fromStart = function(topY,height){
		if(arguments.length>0){
			//alert(topY);
			topY = parseInt(topY)*parseInt(height);
			this.programsScroll.scrollTo(0, -topY, 0);
		}else{
			this.programsScroll.scrollTo(0, 0, 0);
		}
	};
	
	this.newRefresh = function(totalCount){
		if(arguments.length>0){
			this.pageCount = totalCount;
		}
		//this.currentPage = 1;
		if(this.pageCount == "0"){
			$('#' + this.config.pullUp.name).hide();
			$('#' + this.config.emptyDiv.name).html("");
			$('#' + this.config.emptyDiv.name).show();
			var emptyDivId = this.config.emptyDiv.name;
			var text = this.config.emptyDiv.sorry;
			setTimeout(function(){
				$('#' + emptyDivId).html(text);
			},500);
		}else{
			$('#' + this.config.emptyDiv.name).hide();
			$('#' + this.config.pullUp.name).show();
			this.hideNoMore();
		}
		this.refershDiv();
		this.fromStart();
	};
	this.hideEmpty = function(){
		$('#' + this.config.emptyDiv.name).hide();
	};
	this.showUpMore = function(){
		$('#' + this.config.pullUp.all).show();
		$('#' + this.config.pullUp.all).html(this.config.Up.More);
	};
	this.showNoMore = function(){
		$('#' + this.config.pullUp.name).show();
		var ele = document.getElementById(this.config.pullUp.label);
		ele.innerHTML = this.config.Up.NoMore;
		//$('#' + this.config.pullUp.label).html(this.config.Up.NoMore);
	};
	this.setCallback = function(callback){
		this.callback = callback;
		this.callback.call(this,1);
	};
	
	this.showFilter=function(pageNo,data){
		pullDownEl = document.getElementById(this.config.pullDown.name);
		pullUpEl = document.getElementById(this.config.pullUp.name);
		pullDownOffset = pullDownEl.offsetHeight;
		pullUpOffset = pullUpEl.offsetHeight;
		if(pageNo == 1) {
            $('#'+this.config.result.name).html(data);
        } else{
            $('#'+this.config.result.name).append(data);
        }
		
		$("#"+this.config.loading.name).show();
		
		$("#"+this.config.loading.name).hide();
		//this.pageCount=data.pageCount;
		//alert(this.pageCount);
		this.currentPage=pageNo;
		if(this.currentPage == 1 ){
            this.programsScroll.y = pullUpOffset;
        }
		
        $('#'+this.config.pullUp.name).css('display','block');
				
        if(this.currentPage < this.pageCount){
            $('#'+this.config.pullUp.all).html(this.config.Up.More);
        }else{
            $('#'+this.config.pullUp.all).html(this.config.Up.NoMore);		
        }
		setTimeout(this.refershDiv.call(this),3000);
	};
	//this.load(this);
	//setTimeout(this.refershDiv,500);
	this.init = function(){
		document.getElementById(this.config.divName.name).addEventListener('touchmove', function (e) {e.preventDefault();}, false);
	    document.getElementById(this.config.divName.name).addEventListener('DOMContentLoaded', function () {}, false);
	}
	//this.showFilter(1);
	
}


